package ca.usherbrooke.domus.sipnmove;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.ServerSocket;
import java.net.Socket;

/**
 * Observes TCP messages on a given port and display it in the console
 */
public class Snoopy {

	/**
	 * @param args
	 */
	public static void main(String[] args) {

		/*********************************************
		 * Input parameters management
		 *********************************************/
		
		if (args.length < 1) {
			System.err.println("Not enough arguments :");
			System.err.println();
			System.err.println("[command] port_number");
			System.err.println("	port_number = Local port number");
			System.exit(-1);
		}

		try {
			
			ServerSocket serverSocket = new ServerSocket(new Integer(args[0]));
			Socket socket = serverSocket.accept();

			//PrintWriter outputBuffer = new PrintWriter(socket.getOutputStream(), true);
			BufferedReader inputBuffer = new BufferedReader(new InputStreamReader(socket.getInputStream()));

			String message;
			while(true) {
				message = inputBuffer.readLine();
				System.out.println(message);
			}

			
		} catch (NumberFormatException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

}
